package com.example.quartz.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * @ClassName: ResponseMessage
 * @Description: TODO：统一响应
 * @Author: Lenovo
 * @Data: 14:23 2021/2/26
 * @Version: 1.0
 */


@Data
@ApiModel(value = "ResponseMessage")
public class ResponseMessage<T> implements Serializable {

    @ApiModelProperty(value = "成功标识；SUCCESS：成功；ERROR:失败")
    protected String code;

    @ApiModelProperty(value = "描述信息")
    protected String msg;

    @ApiModelProperty(value = "返回数据")
    protected T result;

    @ApiModelProperty(value = "当前页", example = "10")
    private int pageNum;

    @ApiModelProperty(value = "总条数", example = "10")
    private int total;


    public static <T> ResponseMessage<T> error() {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = "失败";
        responseMessage.code("ERROR");
        return responseMessage;
    }


    public static <T> ResponseMessage<T> error(String msg) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = msg;
        responseMessage.code("ERROR");
        return responseMessage;
    }


    public static <T> ResponseMessage<T> errorMsg(String msg, T result) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = msg;
        responseMessage.code("ERROR");
        if (result != null) {
            responseMessage.result = result;
        } else {
            responseMessage.result = (T) new LinkedHashMap<>();
        }
        return responseMessage;
    }


    public static <T> ResponseMessage<T> errorMsg(T result) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = "失败";
        responseMessage.code("ERROR");
        if (result != null) {
            responseMessage.result = result;
        } else {
            responseMessage.result = (T) new LinkedHashMap<>();
        }
        return responseMessage;
    }


    public static <T> ResponseMessage<T> ok() {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = "成功";
        responseMessage.code("SUCCESS");
        return responseMessage;
    }


    public static <T> ResponseMessage<T> ok(String msg) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = msg;
        responseMessage.code("SUCCESS");
        return responseMessage;
    }


    public static <T> ResponseMessage<T> ok(String msg, T result) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = msg;
        responseMessage.code("SUCCESS");
        if (result != null) {
            responseMessage.result = result;
        } else {
            responseMessage.result = (T) new LinkedHashMap<>();
        }
        return responseMessage;
    }


    public static <T> ResponseMessage<T> ok1(String msg, T result) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = msg;
        responseMessage.code("SUCCESS");
        responseMessage.result = result;
        return responseMessage;
    }


    public static <T> ResponseMessage<T> ok(T result) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = "成功";
        responseMessage.code("SUCCESS");
        if (result != null) {
            responseMessage.result = result;
        } else {
            responseMessage.result = (T) new LinkedHashMap<>();
        }
        return responseMessage;
    }


    public static <T> ResponseMessage<T> ok(T result, int pageNum, int total) {
        ResponseMessage<T> responseMessage = new ResponseMessage<>();
        responseMessage.msg = "成功";
        responseMessage.code("SUCCESS");
        if (result != null) {
            responseMessage.result = result;
        } else {
            responseMessage.result = (T) new LinkedHashMap<>();
        }
        responseMessage.pageNum = pageNum;
        responseMessage.total = total;
        return responseMessage;
    }

    public ResponseMessage<T> code(String code) {
        this.code = code;
        return this;
    }
}
